<?php
namespace backend\controllers;

use Yii;
use backend\models\Post;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;

use yii\filters\VerbFilter;
use yii\filters\AccessControl;


use yii\data\Pagination;
use backend\base\BaseBackendController;
use base\YiiMobo;

/**
 * Site controller
 */
class PostController extends BaseBackendController
{
	public $layout = false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                //'only' => ['logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout', 'index', 'create'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }



    public function actionIndex()
    {
		//exit(\yii\web\UrlRule::createUrl('post/update', ['id'=>'2', 'modelid'=>1]));
		//exit(Yii::$app->urlManager->createUrl('post/update', ['id'=>'2', 'modelid'=>1]));
		//Yii::$app->redis->set('a', [1,23,4]);
       // Yii::$app->redis->executeCommand('HMSET', ['user:1', 'name', 'joe', 'solary', 2000]);
		//var_dump(Yii::$app->redis->executeCommand('GET'));
		//var_dump(Yii::$app->redis->get('a'));
		//exit;
		$query = Post::find();
		$keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : false;
		if ($keyword) $query->where('title');
        $sql = 'select * from {{%post}}';  
         
        return $this->render('index', YiiMobo::pageRows($sql));
		
       
    }
    public function actionIndex2()
    {
        return $this->render('index');
    }
	
	
    public function actionCreate()
    {
        $model = new Post;
		$sql = "select * from {{%forum}}";
        $forum = Yii::$app->db->createCommand($sql)->queryAll();
		Yii::$app->FileCache->set('forum', $forum);
		//print_r($_POST['Post']);
		//exit;
		if ($model->load(Yii::$app->request->post()) && $model->save())
		{
			
			$this->OperTips(['backurl'=> \Yii::$app->urlManager->createUrl('post/index')]);
		}
		else
		{
			$data = [
			    'model' => $model,
				'forum' => $forum
			]; 
			return $this->render('_form', $data);
		}
		
    }
	
    public function actionUpdate($id)
    {
        $model = self::findModel($id);
		$sql = "select * from {{%forum}}";
        $forum = Yii::$app->db->createCommand($sql)->queryAll();  
		//print_r($_POST['Post']);
		//exit;
		if ($model->load(Yii::$app->request->post()) && $model->save())
		{
			
			$this->OperTips(['backurl'=> \Yii::$app->urlManager->createUrl('post/index')]);
		}
		else
		{
			$data = [
			    'model' => $model,
				'forum' => $forum
			]; 
			return $this->render('_form', $data);
		}
		
    }
	protected function save(){
		
	}
	
	
    protected function findModel($id = '')
    {
        if (($model = Post::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
